package com.sc.service;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.sc.dal.common.pagination.Constants;
import com.sc.dal.common.pagination.Page;
import com.sc.dal.common.pagination.PaginationBean;

public class BaseServiceImpl<T> implements BaseService<T> {
	
	@Autowired
	protected SqlSessionFactoryBean sqlSessionFactory;
	
	public PaginationBean<T> getPagingDatas(T o,Integer currPage,Integer pageSize,String countMethod,String queryMethod,Class<?> cls) throws Exception{
		Page page = new Page();
		if(currPage==null || currPage==0){
			currPage = 1;
		}
		if(pageSize==null){
			pageSize = Constants.PAGESIZE;
		}
		page.setCurrPage(currPage);
		page.setPageSize(pageSize);
		
		String mapperString = cls.getName();
		
		String count = mapperString + "." + countMethod;
        String query = mapperString + "." + queryMethod;
		
		return queryForListByPagination(o,page,count,query);
	}
	
	/**
	  * <h2>重载的分页查询，分页信息采用对象传递</h2>
	  * @Title: getPagingDatas
	  * @Description: 重载的分页查询，分页信息采用对象传递
	  * @param @param o
	  * @param @param page 封装分页信息（总页数，总记录数，当前页，每页记录数）
	  * @param @param count
	  * @param @param query
	  * @param @throws Exception
	  * @return PaginationBean<T>    返回类型
	  * @throws Exception
	 */
	public PaginationBean<T> getPagingDatas(T o,Page page,String countMethod,String queryMethod,Class<?> cls) throws Exception{
		if(page.getCurrPage()==0){
			page.setCurrPage(1);
		}
		if(page.getPageSize()==0){
			page.setPageSize(Constants.PAGESIZE);
		}
		
		String mapperString = cls.getName();
		
		String count = mapperString + "." + countMethod;
        String query = mapperString + "." + queryMethod;
		
		return queryForListByPagination(o,page,count,query);
	}
	
	public PaginationBean<T> queryForListByPagination(T o, Page page,String count,String query) throws Exception {
		PaginationBean<T> pageHolder = null;
		SqlSession        session    = null;
		try {
			SqlSessionFactory sessionFactory = sqlSessionFactory.getObject();
            session = SqlSessionUtils.getSqlSession(sessionFactory);
	        int totalRecords = (Integer) session.selectOne(count, o);
	        pageHolder = new PaginationBean<T>(page, totalRecords);
//	        StopWatch sw = new StopWatch();
//	        sw.start();
	        List<T> resultList = queryForList(session,o,
	        		(pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(), pageHolder.getPageSize(),query);
	        
//	        sw.stop();
//	        System.out.println(sw.getTime()/1000);
	        pageHolder.setPageList(resultList);
		}finally{
        	if(session!=null){
        		session.close();
        	}
        }
        return pageHolder;
    }
    
    private List<T> queryForList(SqlSession session,T o, int offSet, int maxRow,String query) {
        return session.selectList(query, o, new RowBounds(offSet, maxRow));
    }
	
}
